/* ===================================
Michal Kowalczyk
2010 EZI
==================================== */

#include "stdafx.h"
#include "engine.h"
#include "mos3d.h"
#include "views.h"

void ViewListener::handleNewActorSignal(int id)
{
	EntityPtr tmpEntity(entityManager.getEntity(id));
	if(tmpEntity->getType() == "inversionlayer")
	{
		boost::shared_ptr<InversionLayerView> newInvLay(new InversionLayerView(tmpEntity->getPosition3D(), id));
		newInvLay->setup();
		renderer.attachView(id, newInvLay);
	}

	else if (tmpEntity->getType() == "electron")
	{
		boost::shared_ptr<ElectronView> newActor(new ElectronView(tmpEntity->getPosition3D(), id));
		renderer.attachView(id, newActor);
	}	
	else if (tmpEntity->getType() == "hole")
	{
		boost::shared_ptr<HoleView> newActor(new HoleView(tmpEntity->getPosition3D(), id));
		renderer.attachView(id, newActor);
	}	
}
CollisionInfo ViewListener::handleActorMovedSignal(int id)
{
	EntityPtr tmpEntity(entityManager.getEntity(id));
	CollisionInfo tmpCollisionInfo;
	size = entityManager.getSize();
	xdist = 0.0f;
	zdist = 0.0f;
	distance = 0.0f;
	for(int i = 1; i <= size; i++)
	{
		if( i != id )
		{
			EntityPtr tmpCollision(entityManager.getEntity(i));
			xdist = (float)(abs(tmpEntity->getPosition3D().x - tmpCollision->getPosition3D().x));
			if(xdist > deltaCollision)
				continue;
			else
			{
				zdist = (float)(abs(tmpEntity->getPosition3D().z - tmpCollision->getPosition3D().z));
				if(zdist > deltaCollision)
					continue;
				else
				{
					tmpCollisionInfo.withWhat = tmpCollision->getId();
					tmpCollisionInfo.collision = true;
					return tmpCollisionInfo;
					//distance = (float)sqrt( (xdist*xdist) + (zdist*zdist) );
					//STLogger::Instance().log("distance: ", (int)distance, 0);
					//	if(distance < 2.5f)
				}
			}
		}
	}
	return tmpCollisionInfo;
}

bool InversionLayerView::setup()
{
	invlay =  dynamic_cast<InversionLayer *>(&*EntityManager::Instance().getEntity(m_id));
	width = invlay->getWidth();
	return true;
}
void InversionLayerView::update()
{
	width = invlay->getWidth();
	BaseView::update();
}

void InversionLayerView::render()
{
	glPushMatrix();
	glTranslatef(m_position.x, m_position.y, m_position.z);
	glColor3f(1.0f, 0.45f, 0.63f);

	glBegin(GL_QUADS);						// Draw A Quad
	glVertex3f(m_position.x, m_position.y, m_position.z);				// Top Left
	glVertex3f(m_position.x+50.0f, m_position.y, m_position.z);				// Top Right
	glVertex3f(m_position.x+50.0f, m_position.y, m_position.z-width);				// Bottom Right
	glVertex3f(m_position.x, m_position.y, m_position.z-width);				// Bottom Left
	glEnd();	

	glPopMatrix();

}

void ElectronView::render()
{
	glPushMatrix();
	glTranslatef(m_position.x, m_position.y+5.0f, m_position.z);
	glRotatef(m_position.angle, m_position.rotx, m_position.roty, m_position.rotz);
	glDisable(GL_BLEND);
	glColor3f(0.2f, 0.8f, 0.9f);	
	glBegin(GL_QUADS);		
	glNormal3f( 0.0f, 1.0f, 0.0f);		
	//glColor3f(1.0f,0.5f,0.0f);			// Set The Color To Orange
	glNormal3f( 0.0f, -1.0f, 0.0f);	
	glVertex3f( 1.0f,-1.0f, 1.0f);			// Top Right Of The Quad (Bottom)
	glVertex3f(-1.0f,-1.0f, 1.0f);			// Top Left Of The Quad (Bottom)
	glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Bottom)
	glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Bottom)
	//	glColor3f(1.0f,1.0f,1.0f);			// 
	glNormal3f( 0.0f, 0.0f, 1.0f);	
	//glColor3f(1.0f,1.0f,0.0f);			// Set The Color To Yellow
	glNormal3f( 0.0f, 0.0f, -1.0f);	
	glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Back)
	glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Back)
	glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Back)
	glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Back)
	//	glColor3f(0.0f,0.0f,1.0f);			// Set The Color To Blue
	glNormal3f( -1.0f, 0.0f, 0.0f);	
	glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Left)
	glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Left)
	glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Left)
	glVertex3f(-1.0f,-1.0f, 1.0f);			// Bottom Right Of The Quad (Left)
	//glColor3f(1.0f,0.0f,1.0f);			// Set The Color To Violet
	glNormal3f( 1.0f, 0.0f, 0.0f);	
	glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Right)
	glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Right)
	glVertex3f( 1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Right)
	glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Right)
	glEnd();
	glEnable(GL_BLEND);
	glPopMatrix();
}



void HoleView::render()
{
	glPushMatrix();
	glTranslatef(m_position.x, m_position.y+5.0f, m_position.z);
	glRotatef(m_position.angle, m_position.rotx, m_position.roty, m_position.rotz);
	glDisable(GL_BLEND);
	glColor3f(0.8f,0.0f,0.7f);			// Red
	glBegin(GL_TRIANGLES);
	glNormal3f( 0.0f, 0.0f, 1.0f);
	glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Front)
	glVertex3f(-1.0f,-1.0f, 1.0f);			// Left Of Triangle (Front)
	glVertex3f( 1.0f,-1.0f, 1.0f);			// Right Of Triangle (Front)

	glNormal3f( 1.0f, 0.0f, 0.0f);
	glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Right)
	glVertex3f( 1.0f,-1.0f, 1.0f);			// Left Of Triangle (Right)
	glVertex3f( 1.0f,-1.0f, -1.0f);			// Right Of Triangle (Right)

	glNormal3f( 0.0f, 1.0f, 0.0f);
	glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Back)
	glVertex3f( 1.0f,-1.0f, -1.0f);			// Left Of Triangle (Back)
	glVertex3f(-1.0f,-1.0f, -1.0f);			// Right Of Triangle (Back)

	glNormal3f( 0.0f, 1.0f, 0.0f);
	glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Left)
	glVertex3f(-1.0f,-1.0f,-1.0f);			// Left Of Triangle (Left)
	glVertex3f(-1.0f,-1.0f, 1.0f);			// Right Of Triangle (Left)

	glEnd();
	glEnable(GL_BLEND);
	glPopMatrix();
}